package com.dcdata.haixin.client;

import ch.qos.logback.core.encoder.ByteArrayUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.io.*;
import java.net.InetSocketAddress;

/**
 * @author: xinmi
 * @date: 2020/11/26
 * @description:
 */
public class HxTcpClient {

    private byte[] loginBytes = new byte[32];

    public void runClient() throws Exception{
        //如果现在客户端不同，那么也可以不实用多线程模式来处理
        //在netty中考虑到代码的统一性，也允许你在客户端设置线程池
        EventLoopGroup group = new NioEventLoopGroup();//创建一个线程池
        try {
            Bootstrap client = new Bootstrap();//创建客户毒案处理程序
            client.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY,true) //允许接收大块的返回数据
                    .handler(new ChannelInitializer<SocketChannel>() {

                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            socketChannel.pipeline().addLast(new FixedLengthFrameDecoder(1000));
                            socketChannel.pipeline().addLast(new Message2ByteEncoder());
                            socketChannel.pipeline().addLast(new Byte2MessageDecoder());
                            socketChannel.pipeline().addLast(new TcpClientHandler());
                        }
                    });
            //ChannelFuture channelFuture = client.connect(new InetSocketAddress("103.242.175.154", 8002)).sync();//等待连接处理
            ChannelFuture channelFuture = client.connect(new InetSocketAddress("127.0.0.1", 8002)).sync();//等待连接处理
            channelFuture.addListener(new GenericFutureListener() {
                @Override
                public void operationComplete(Future future) throws InterruptedException, IOException {
                    if(future.isSuccess()){
                        //while (true) {
                            /*Thread.sleep(500);
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e00200101000c0203010301000001ff0603020201000001ff03030104033e7f"));
                            System.out.println("服务器连接已经完成，可以确保进行消息准确传输");


                            Thread.sleep(500);
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e00200101000c0203010301000001ff0603030201000001ff03030104033e7f"));

                            Thread.sleep(500);
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e00170108000c0301000001ff09014b1643f30103017f"));

                            Thread.sleep(500);
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e00170108000c0301000001ff09014b1643f30103017f"));

                            Thread.sleep(500);
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e005b010300120101000000280203010301030b49ad080367010200780201000434240302001f040100089ab505020000070259f40801552cff180a03000b0203950c03060d01000000280e01000004320f0300110100e983447f"));

                            Thread.sleep(500);
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e0079010400120101000000280203010301030b49ad0803680103010203210301000000280403030503010601552cff1f0703010803030901000000280a03000b03000c03220d0200780e01000432670f02001f1001000899771202036713025a5514020367150100000432160306170300180100e983727f"));
*/
/*
                        Thread.sleep(500);
                        channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray("7e009e0103001b010100064dc0020301030104c4d98806030208037a09015fcf2f38010200710201000cf743030200160401000c0bee070268b008015fcf2f2c0c03030d0100064dc00e01000000401101046792eaa0080071000cf7430016000c0bee000068b05fcf2f2c030000c30340c40100200020c50301c60302c70304c8041032392f4348494e415f54454c45434f4dc904023138ca040232307f"));
*/


                        String fileName = System.getProperty("user.dir") + "/APTSDataForwardService_data_20201208154827.log";
                        FileInputStream fis = new FileInputStream(fileName);

                        //Construct BufferedReader from InputStreamReader
                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

                        String line = null;
                        while ((line = br.readLine()) != null) {
                            channelFuture.channel().write(ByteArrayUtil.hexStringToByteArray(line));

                            Thread.sleep(500);
                        }

                        br.close();

                        //}
                    }
                }
            });
            channelFuture.channel().closeFuture().sync();  //等待关闭连接

        }finally {
            group.shutdownGracefully();
        }
    }



    public static void main(String[] args) throws Exception {

        new HxTcpClient().runClient();


    }

    private static void readFile1(String fin) throws IOException {

    }
}
